package com.shopcart.service.impl;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.demo.dialet.PaginationSupport;
import com.demo.util.DateUtil;
import com.demo.util.JSONUtils;
import com.demo.util.SqlUtils;
import com.demo.util.StringUtil;
import com.h5.Ret;
import com.shopcart.daoEx.model.StoreCommentEx;
import com.shopcart.domain.StoreComment;
import com.shopcart.repository.StoreCommentRepository;
import com.shopcart.service.StoreCommentService;
import com.zy.store.proto.MOAppStore.MComment;
import com.zy.store.proto.MOAppStore.MCommentList;
import com.zy.store.proto.MOAppStore.MMyComment;
import com.zy.store.proto.MOAppStore.MMyCommentList;

@Service
@Transactional
public class StoreCommentServiceImpl implements StoreCommentService {

	@SuppressWarnings("unused")
	private static final Logger LOGGER = LoggerFactory
			.getLogger(StoreCommentServiceImpl.class);
	
	@Autowired
	JdbcTemplate jdbcTemplate;
	
	@Autowired
	private StoreCommentRepository storeCommentRepository;
	
	@Override
	public List<StoreCommentEx> selectExList(String search,String areaCode){
		String sql ="select sc.*,s.name as storeName,u.nick_name as nickName,u.image as headImg from s_store_comment sc "
				+ "left join s_store s on sc.store_id = s.id  "
				+ "left join f_user u on u.id = sc.user_id "
				+ "where 1=1 ";
		if(StringUtils.isNotBlank(search)){
			sql+=" and sc.content like '%"+search+"%'";
		}
		if(StringUtils.isNotBlank(areaCode)){
			sql+=" and s.area_code like '"+areaCode+"%'";
		}
		sql+=" order by sc.create_time desc";
		
		int c = jdbcTemplate.queryForObject("select count(1) from ("+ sql+") a_cnt", Integer.class);
		PaginationSupport.getContext().setTotalCount(c);
		
		sql+=" limit "+(PaginationSupport.getContext().getStartRowNo()-1)+","+PaginationSupport.getContext().getPageLength();
		
		List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
		List<StoreCommentEx> exList = new ArrayList<>();
		for (Map<String, Object> map : maps) {
			StoreCommentEx ex = new StoreCommentEx();
			SqlUtils.transMap2Bean2(map, ex);
			exList.add(ex);
		}
		return exList;
	}
	
	@Override
	public List<StoreCommentEx> selectExList(String search,String areaCode,String storeId){
		String sql ="select sc.*,s.name as storeName,u.nick_name as nickName,u.image as headImg from s_store_comment sc "
				+ "left join s_store s on sc.store_id = s.id  "
				+ "left join f_user u on u.id = sc.user_id "
				+ "where 1=1 ";
		if(StringUtils.isNotBlank(search)){
			sql+=" and sc.content like '%"+search+"%'";
		}
		if(StringUtils.isNotBlank(areaCode)){
			sql+=" and s.area_code like '"+areaCode+"%'";
		}
		if(StringUtils.isNotBlank(storeId)){
			sql+=" and s.id = '"+storeId+"'";
		}
		sql+=" order by sc.create_time desc";
		
		int c = jdbcTemplate.queryForObject("select count(1) from ("+ sql+") a_cnt", Integer.class);
		PaginationSupport.getContext().setTotalCount(c);
		
		sql+=" limit "+(PaginationSupport.getContext().getStartRowNo()-1)+","+PaginationSupport.getContext().getPageLength();
		
		List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
		List<StoreCommentEx> exList = new ArrayList<>();
		for (Map<String, Object> map : maps) {
			StoreCommentEx ex = new StoreCommentEx();
			SqlUtils.transMap2Bean2(map, ex);
			exList.add(ex);
		}
		return exList;
	}
	

	@Override
	public MCommentList.Builder findByStoreId(String storeId,Integer page,Integer limit) {
		MCommentList.Builder ret = MCommentList.newBuilder();
		String sql = "select sc.*,u.nick_name,u.image,u.id as userId from s_store_comment sc "
				+ "	left join f_user u on sc.user_id = u.id  where sc.store_id="+storeId
				+ " order by sc.create_time desc limit "+page*limit+","+(page+1)*limit;
		List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
		
		for (Map<String, Object> map : maps) {
			MComment.Builder m = MComment.newBuilder();
			m.setContent(StringUtil.obj2String(map.get("content")));
			m.setHeadImg(StringUtil.obj2String(map.get("image")));
			m.setNickName(StringUtil.obj2String(map.get("nick_name")));
			m.setStar(map.get("score")==null?0:new Double(StringUtil.obj2String(map.get("score"))));
			m.setTime(DateUtil.dateAsQQ(DateUtil.strToDate(StringUtil.obj2String(map.get("create_time")))));
			m.setUserid(StringUtil.obj2String(map.get("userId")));
			String imgs = StringUtil.obj2String(map.get("imgs"));
			List<String> imgArr = Arrays.asList(imgs.split(","));
			for (String str : imgArr) {
				m.addImgs(str);
			}
			ret.addList(m);
		}
		return ret;
	}
	
	@Override
	public MMyCommentList.Builder myComment(String userId,Integer page,Integer limit) {
		MMyCommentList.Builder ret = MMyCommentList.newBuilder();
		String sql = "select  sc.*,u.nick_name,u.image,u.id as userId,s.name as storeName,s.logo,s.id as storeId from s_store_comment sc "
				+" left join s_store s on s.id = sc.store_id"
				+" left join f_user u on sc.user_id = u.id"
				+" where sc.user_id = '"+userId
				+ "' order by sc.create_time desc limit "+page*limit+","+(page+1)*limit;
		List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
		
		for (Map<String, Object> map : maps) {
			MMyComment.Builder m = MMyComment.newBuilder();
			m.setStoreId(StringUtil.obj2String(map.get("storeId")));
			m.setLogo(StringUtil.obj2String(map.get("logo")));
			m.setStoreName(StringUtil.obj2String(map.get("storeName")));
			MComment.Builder c = MComment.newBuilder();
			c.setContent(StringUtil.obj2String(map.get("content")));
			c.setId(StringUtil.obj2String(map.get("id")));
			c.setStar(map.get("score")==null?0:new Double(StringUtil.obj2String(map.get("score"))));
			c.setTime(DateUtil.dateAsQQ(DateUtil.strToDate(StringUtil.obj2String(map.get("create_time")), "yyyy-MM-dd HH:mm:ss")));
			
			String imgs = StringUtil.obj2String(map.get("imgs"));
			List<String> imgArr = Arrays.asList(imgs.split(","));
			for (String str : imgArr) {
				c.addImgs(str);
			}
			
			m.setComment(c);
			
			ret.addList(m);
		}
		return ret;
	}
	@Override
	public int countByStoreId(String id) {
		return storeCommentRepository.commentCount(id);
	}
	
	@Override
	public StoreComment findById(String id) {
		return storeCommentRepository.findOne(id);
	}
	
	@Override
	public void deleteById(String id) {
		storeCommentRepository.delete(id);
	}


	@Override
	public void save(StoreComment storeComment) {
		storeComment.setId(StringUtil.getUUID());
		storeCommentRepository.save(storeComment);
	}
	@Override
	public String myCommentList(String userId,Integer page,Integer limit){
		String sql = "select  sc.*,u.nick_name,u.image,u.id as userId,s.name as storeName,s.logo,s.id as storeId from s_store_comment sc "
				+" left join s_store s on s.id = sc.store_id"
				+" left join f_user u on sc.user_id = u.id"
				+" where sc.user_id = '"+userId
				+ "' order by sc.create_time desc limit "+page*limit+","+(page+1)*limit;
		List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
		JSONArray ret = new JSONArray();
		for (Map<String, Object> map : maps) {
			JSONObject object = new JSONObject();
			JSONObject c = new JSONObject();
			object.put("storeId", map.get("storeId"));
			object.put("logo", map.get("logo"));
			object.put("storeName", map.get("storeName"));
			c.put("content", map.get("content"));
			c.put("id", map.get("id"));
			c.put("star", map.get("score")==null?0:new Double(StringUtil.obj2String(map.get("score"))));
			c.put("time", DateUtil.dateAsQQ(DateUtil.strToDate(StringUtil.obj2String(map.get("create_time")), "yyyy-MM-dd HH:mm:ss")));
			String imgs = StringUtil.obj2String(map.get("imgs"));
			List<String> imgArr = Arrays.asList(imgs.split(","));
			c.put("imgs", imgArr);
			object.put("comment", c);
			ret.add(object);
		}
		return JSONUtils.serialize(ret);
	}
	@Override
	public String findByStore(String storeId,Integer page,Integer limit){
		String sql = "select sc.*,u.nick_name,u.image,u.id as userId from s_store_comment sc "
				+ "	left join f_user u on sc.user_id = u.id  where sc.store_id='"+storeId
				+ "' order by sc.create_time desc limit "+page*limit+","+(page+1)*limit;
		List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
		for (Map<String, Object> map : maps) {
			String imgs = StringUtil.obj2String(map.get("imgs"));
			if (StringUtils.isNotBlank(imgs)){
				map.put("images", imgs.split(","));
			}
		}
		int c = countByStoreId(storeId);

		return JSONUtils.serialize(new Ret(c,maps));
	}
	
}
